package inabit.inabitcompiler.productions.variable.types;

import inabit.compiler.parser.grammar.NonTerminal;
import inabit.compiler.parser.grammar.Production;
import inabit.compiler.parser.grammar.Symbol;
import inabit.compiler.parser.grammar.Terminal;
import inabit.inabitcompiler.lexical.InabitTokenClass;
import inabit.inabitcompiler.parser.InabitSemanticAction;

/**T -> discrete(N, N)*/
/**@author Rafael Rocha*/
public class T_Rule2 extends Production{
	private NonTerminal int_cte_min;
	private NonTerminal int_cte_max;
	
	public T_Rule2() {
		super("T");
	}

	@Override
	protected Symbol[] production(final NonTerminal nonTerminal) {
		int_cte_min = new NonTerminal("N");
		int_cte_max = new NonTerminal("N");
		
		Symbol[] symbols = {
			new Terminal(InabitTokenClass.DISCRETE),
			new Terminal(InabitTokenClass.OPEN_PARENTHESIS),
			int_cte_min,
			new Terminal(InabitTokenClass.COMMA),
			int_cte_max,
			new Terminal(InabitTokenClass.CLOSE_PARENTHESIS),
			new InabitSemanticAction(nonTerminal) {
				
				@Override
				public void execute() throws Exception {
					String min = int_cte_min.code;
					String max = int_cte_max.code;
					try{
						Long.parseLong(min);
						Long.parseLong(max);
					}catch(NumberFormatException e){
						throw new Exception("Value must be an integer");
					}
					nonTerminal.code = "discrete:"+min + ","+max;
				}
			}
		};
		return symbols;
	}
}
